<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="登记号" prop="registerCode">
        <el-input
          v-model="queryParams.registerCode"
          placeholder="请输入登记号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:register:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:register:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:register:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:register:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="registerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="登记号" align="center" prop="registerCode" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="持有人" align="center" prop="holder" />
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="农药类型" align="center" prop="medicineType" />
      <el-table-column label="总有效成分含量" align="center" prop="effectiveContent" />
      <el-table-column label="剂型" align="center" prop="dosageForm" />
      <el-table-column label="场所" align="center" prop="place" />
      <el-table-column label="防治对象" align="center" prop="controlTargets" />
      <el-table-column label="施用方法" align="center" prop="method" />
      <el-table-column label="毒性" align="center" prop="toxicity" />
      <el-table-column label="有效成分1" align="center" prop="activeIngredient1" />
      <el-table-column label="英文1" align="center" prop="english1" />
      <el-table-column label="含量1" align="center" prop="content1" />
      <el-table-column label="有效成分2" align="center" prop="activeIngredient2" />
      <el-table-column label="英文2" align="center" prop="english2" />
      <el-table-column label="含量2" align="center" prop="content2" />
      <el-table-column label="有效成分3" align="center" prop="activeIngredient3" />
      <el-table-column label="英文3" align="center" prop="english3" />
      <el-table-column label="含量3" align="center" prop="content3" />
      <el-table-column label="有效起始日" align="center" prop="effectiveStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectiveStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效结束日" align="center" prop="effectiveEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectiveEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="${comment}" align="center" prop="deadlineStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadlineStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="${comment}" align="center" prop="deadlineEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadlineEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:register:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:register:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改中药登记对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="登记号" prop="registerCode">
          <el-input v-model="form.registerCode" placeholder="请输入登记号" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="持有人" prop="holder">
          <el-input v-model="form.holder" placeholder="请输入持有人" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="总有效成分含量">
          <editor v-model="form.effectiveContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="剂型" prop="dosageForm">
          <el-input v-model="form.dosageForm" placeholder="请输入剂型" />
        </el-form-item>
        <el-form-item label="场所" prop="place">
          <el-input v-model="form.place" placeholder="请输入场所" />
        </el-form-item>
        <el-form-item label="防治对象" prop="controlTargets">
          <el-input v-model="form.controlTargets" placeholder="请输入防治对象" />
        </el-form-item>
        <el-form-item label="施用方法" prop="method">
          <el-input v-model="form.method" placeholder="请输入施用方法" />
        </el-form-item>
        <el-form-item label="毒性" prop="toxicity">
          <el-input v-model="form.toxicity" placeholder="请输入毒性" />
        </el-form-item>
        <el-form-item label="有效成分1" prop="activeIngredient1">
          <el-input v-model="form.activeIngredient1" placeholder="请输入有效成分1" />
        </el-form-item>
        <el-form-item label="英文1" prop="english1">
          <el-input v-model="form.english1" placeholder="请输入英文1" />
        </el-form-item>
        <el-form-item label="含量1" prop="content1">
          <el-input v-model="form.content1" placeholder="请输入含量1" />
        </el-form-item>
        <el-form-item label="有效成分2" prop="activeIngredient2">
          <el-input v-model="form.activeIngredient2" placeholder="请输入有效成分2" />
        </el-form-item>
        <el-form-item label="英文2" prop="english2">
          <el-input v-model="form.english2" placeholder="请输入英文2" />
        </el-form-item>
        <el-form-item label="含量2" prop="content2">
          <el-input v-model="form.content2" placeholder="请输入含量2" />
        </el-form-item>
        <el-form-item label="有效成分3" prop="activeIngredient3">
          <el-input v-model="form.activeIngredient3" placeholder="请输入有效成分3" />
        </el-form-item>
        <el-form-item label="英文3" prop="english3">
          <el-input v-model="form.english3" placeholder="请输入英文3" />
        </el-form-item>
        <el-form-item label="含量3" prop="content3">
          <el-input v-model="form.content3" placeholder="请输入含量3" />
        </el-form-item>
        <el-form-item label="有效起始日" prop="effectiveStartTime">
          <el-date-picker clearable
            v-model="form.effectiveStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效起始日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效结束日" prop="effectiveEndTime">
          <el-date-picker clearable
            v-model="form.effectiveEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效结束日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="deadlineStartTime">
          <el-date-picker clearable
            v-model="form.deadlineStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="deadlineEndTime">
          <el-date-picker clearable
            v-model="form.deadlineEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRegister, getRegister, delRegister, addRegister, updateRegister } from "@/api/product/register";

export default {
  name: "Register",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 中药登记表格数据
      registerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        registerCode: null,
        name: null,
        holder: null,
        province: null,
        medicineType: null,
        effectiveContent: null,
        dosageForm: null,
        place: null,
        controlTargets: null,
        method: null,
        toxicity: null,
        activeIngredient1: null,
        english1: null,
        content1: null,
        activeIngredient2: null,
        english2: null,
        content2: null,
        activeIngredient3: null,
        english3: null,
        content3: null,
        effectiveStartTime: null,
        effectiveEndTime: null,
        deadlineStartTime: null,
        deadlineEndTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询中药登记列表 */
    getList() {
      this.loading = true;
      listRegister(this.queryParams).then(response => {
        this.registerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        registerCode: null,
        name: null,
        holder: null,
        province: null,
        medicineType: null,
        effectiveContent: null,
        dosageForm: null,
        place: null,
        controlTargets: null,
        method: null,
        toxicity: null,
        activeIngredient1: null,
        english1: null,
        content1: null,
        activeIngredient2: null,
        english2: null,
        content2: null,
        activeIngredient3: null,
        english3: null,
        content3: null,
        effectiveStartTime: null,
        effectiveEndTime: null,
        deadlineStartTime: null,
        deadlineEndTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加中药登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRegister(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改中药登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRegister(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegister(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除中药登记编号为"' + ids + '"的数据项？').then(function() {
        return delRegister(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/register/export', {
        ...this.queryParams
      }, `register_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
